﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Jemgine.Data
{
    public partial class RuntimePolygon
    {
        public RuntimePolygon(RuntimePolygon p)
        {
            Data = new List<Vector2>(p.Data);
        }

        public RuntimePolygon()
        {
            Data = new List<Vector2>();
        }

        [ContentSerializerIgnore]
        internal VertexPositionTexture[] VertexCache;

        internal void BuildVertexCache(Matrix TextureMatrix)
        {
            VertexCache = new VertexPositionTexture[Data.Count];


            int StartV = 0;
            int EndV = Data.Count - 1;
            int BufferPos = 0;

            while (StartV <= EndV)
            {
                VertexCache[BufferPos] = new VertexPositionTexture(new Vector3(Data[StartV], 0.0f), 
                    Vector2.Transform(Data[StartV], TextureMatrix));
                ++BufferPos;
                if (StartV != EndV) 
                {
                    VertexCache[BufferPos] = new VertexPositionTexture(new Vector3(Data[EndV], 0.0f), 
                        Vector2.Transform(Data[EndV], TextureMatrix));
                    ++BufferPos; 
                }
                ++StartV;
                --EndV;
            }  
        }

        

        public List<Vector2> Data;

        public int Count { get { return Data.Count; } }
        public Vector2 this[int _index]
        {
            get { return Data[_index]; }
            set { Data[_index] = value; }
        }

        public Vector2[] ToArray() { return Data.ToArray(); }
        public void Add(Vector2 v) { Data.Add(v); }
        public void RemoveAt(int index) { Data.RemoveAt(index); }
        public void Clear() { Data.Clear(); }
        public void Insert(int index, Vector2 v) { Data.Insert(index, v); }
        public void Reverse() { Data.Reverse(); }





        public void RemoveLast()
        {
            RemoveAt(Count - 1);
        }

    }
}
